from monster import Monster
from player import Player
from  overlay import Overlay
from sprites import Generic, Water,Tree, Interaction
from settings import *
from pytmx.util_pygame import  load_pygame

from sprites import WildFlowers
from support import *
from tansition import Transition

#from soli import SoilLayer
class Level:
    def __init__(self):
        self.display = pygame.display.get_surface()#绘制显示
        #创建对象  精灵组
        #self.all_sprites = pygame.sprite.Group()
        self.all_sprites = Gamera()

        self.collisoin_sprites = pygame.sprite.Group()

        self.tree_sprites = pygame.sprite.Group()
        self.interaction_sprite=pygame.sprite.Group()
        self.level_up_sprites = pygame.sprite.Group()
        self.setup()

        self.overlay = Overlay(self.player,self.all_sprites)

        self.transition=Transition(self.reset,self.player)
        #self.soil_layer=SoilLayer(self.all_sprites)
    #加载所有东西
    def setup(self):
        tmx_data=load_pygame('../mygame/data/map.tmx')


        #house 按照创建顺序绘制
        for layer in ['HouseFloor','HouseFurnitureBottom']:
            for x,y,surf in tmx_data.get_layer_by_name(layer).tiles():
               Generic((x * TILE_SIZE, y * TILE_SIZE), surf, self.all_sprites, LAYERS['house bottom'])

        for layer in ['HouseWalls','HouseFurnitureTop']:
            for x,y,surf in tmx_data.get_layer_by_name(layer).tiles():
                Generic((x * TILE_SIZE, y * TILE_SIZE), surf, self.all_sprites, LAYERS['main'])


        #围栏
        for x, y, surf in tmx_data.get_layer_by_name('Fence').tiles():
           Generic((x * TILE_SIZE, y * TILE_SIZE), surf, [self.all_sprites,self.collisoin_sprites])
        #water
        water_frames=import_folder('../mygame/graphics/water')

        for x, y, surf in tmx_data.get_layer_by_name('Water').tiles():
            Water((x * TILE_SIZE, y * TILE_SIZE), water_frames, self.all_sprites)

        #野花object
        for obj in tmx_data.get_layer_by_name('Decoration'):
            WildFlowers((obj.x,obj.y),obj.image,[self.all_sprites,self.collisoin_sprites])
        #tree
        for obj in tmx_data.get_layer_by_name('Trees'):
            Tree(pos=(obj.x, obj.y), surf=obj.image,groups=[self.all_sprites,self.collisoin_sprites,
                                                            self.tree_sprites],
                 name=obj.name,player_add=self.player_add)
        #地图原本碰撞块  不用绘制的实体 模拟地形
        for x,y,surf in tmx_data.get_layer_by_name('Collision').tiles():
            Generic((x*TILE_SIZE,y*TILE_SIZE),pygame.Surface((TILE_SIZE,TILE_SIZE)),self.collisoin_sprites)
        #Player
        for obj in tmx_data.get_layer_by_name('Player'):
            if obj.name=='Start':


                self.player = Player(pos=(obj.x,obj.y),group= self.all_sprites,collision_sprites= self.collisoin_sprites,trees_sprites=self.tree_sprites,interaction_sprites=self.interaction_sprite,levelup=self.level_up_sprites)
                self.monster = Monster(pos=(obj.x, obj.y - 600), group=self.all_sprites, player=self.player)
            if obj.name =='Bed':
                Interaction((obj.x,obj.y),(obj.width,obj.height),self.interaction_sprite,'Bed')

        Generic(
            pos=(0,0),
            surf=pygame.image.load('../mygame/graphics/world/ground.png').convert_alpha(),
            groups=self.all_sprites,
            z=LAYERS['ground']
        )

    def player_add(self,itme):
        self.player.itme_inventory[itme]+=1
    def reset(self):
        for tree in self.tree_sprites.sprites():
            for apple in tree.apple_sprites.sprites():
                apple.kill()

            tree.create_apple()





    def run(self,dt):

        #self.display.fill("black")
        #self.all_sprites.draw(self.display)
        self.all_sprites.custom_draw(self.player)
        self.all_sprites.update(dt)
        self.overlay.display()
        if self.player.sleep:
            self.transition.update()

       # print(self.player.itme_inventory)
#重点的相机类
class Gamera(pygame.sprite.Group):
    def __init__(self):
        super().__init__()
        self.display = pygame.display.get_surface()
        self.offset =pygame.math.Vector2()
        #地图大小
        self.map_width = TILE_WID_NUMBER * TILE_SIZE
        self.map_height = TILE_HEI_NUMBER * TILE_SIZE

    def custom_draw(self,player):
        target_offset_x = player.rect.centerx -SCREEN_WIDTH/2
        target_offset_y=player.rect.centery-SCREEN_HEIGHT/2
        #绘制的更改
        #限制地图边界
        max_offset_x = self.map_width - SCREEN_WIDTH
        min_offset_x = 0
        max_offset_y = self.map_height - SCREEN_HEIGHT
        min_offset_y = 0
        if target_offset_x < min_offset_x:
            self.offset.x = min_offset_x
        elif target_offset_x > max_offset_x:
            self.offset.x = max_offset_x
        else:
            self.offset.x = target_offset_x

            # 对于y轴
        if target_offset_y < min_offset_y:
            self.offset.y = min_offset_y
        elif target_offset_y > max_offset_y:
            self.offset.y = max_offset_y
        else:
            self.offset.y = target_offset_y
        #开始绘制
        for layer in LAYERS.values():
            #排序 绘制 不同的物体叠加  根据有坐标排序绘制 key 是 sprite的y坐标
            for sprite in sorted(self.sprites(),key=lambda sprite: sprite.rect.centery):
                if sprite.z==layer:
                    offset_rect = sprite.rect.copy()
                    offset_rect.centerx -= self.offset.x
                    offset_rect.centery-=self.offset.y
                    #暂时不懂这一句
                    self.display.blit(sprite.image, offset_rect)

                # anaytics

